package com.learn.swagger.controller;

import com.learn.swagger.result.Result;
import com.learn.swagger.vo.User;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 演示接口
 *
 * @author zhousiqi
 * @version 1.0.0
 * @date 2019/2/26 13:36
 */
@RestController()
@RequestMapping(HelloController.PATH)
@Api(tags = "测试swagger")
@Slf4j
public class HelloController {
    static final String PATH = "/hello";

    /**
     * 根据ID查询用户
     *
     * @param id id
     * @return {@link Result}
     */
    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<User> getUserById(@PathVariable(value = "id") Long id) {
        User user = new User();
        user.setSchool("test school");
        user.setAge(20);
        user.setId(id);
        user.setName("xiaoming");

        return Result.ofSuccess("success", user);
    }

    /**
     * 修改用户详细信息
     *
     * @param user user
     * @return {@link Result}
     */
    @ApiOperation(value = "修改用户详细信息", notes = "修改用户详细信息")
    @PutMapping(value = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<User> updateUser(@Valid @ApiParam(value = "修改的用户信息", required = true) @RequestBody User user,
                                   BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return Result.ofFailure(400, bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return Result.ofSuccess("success", user);
    }

    /**
     * 添加用户
     *
     * @param user user
     * @return {@link Result}
     */
    @ApiOperation(value = "添加用户", notes = "添加用户")
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<User> createUser(@Valid @ApiParam(value = "新的用户信息", required = true) @RequestBody User user,
                                   BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.ofFailure(400, bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return Result.ofSuccess("success", user);
    }

    /**
     * 删除用户
     *
     * @param id id
     * @return {@link Result}
     */
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<User> deleteUser(@PathVariable(value = "id") Long id) {
        log.info("删除id为{}的用户", id);
        return Result.ofSuccess("success");
    }
}
